/* Autostop - camera driver
 *
 * Copyright (c) Maciej Gajewski, Grzegorz Latarowski
 * http://code.google.com/p/autostop-automaty-drogowe
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version. NO WARRANTY OF ANY KIND is expressed or implied.
 */
#ifndef VIEWERWINDOW_H
#define VIEWERWINDOW_H

#include <QWidget>
#include <QTcpSocket>
#include <QByteArray>

#include "gnetudp.h"

#include "ui_viewerwindow.h"

class ViewerWindow : public QWidget, protected Ui::ViewerWindow
{
Q_OBJECT
public:
    ViewerWindow(QWidget *parent = 0);

    virtual ~ViewerWindow();

protected slots:

	// button handlers
	virtual void on_buttonPlay_clicked();
	virtual void on_buttonConnect_clicked();
	virtual void on_buttonListenUdp_clicked();
	
	void socketStateChanged( QAbstractSocket::SocketState socketState );
	void socketDataReady();
	void socketError();
	void udpPacketReceived( int packetId, const QByteArray& data );
	void udpPacketsReceived( const QQueue< QPair< quint16, QByteArray> >& packets );
	void requestFrame();
	
protected:

	enum ReciverState {
		WaitingForHeader,
		CollectingData
		};
		
	ReciverState _state;

	void Log( const QString& text );
	void readHeader();
	void readData();
	
	void dataReaded();
	bool segmentedRequested() { return cbSegmented->checkState() == Qt::Checked; }

	QTcpSocket	_socket;
	QByteArray _data;		///< Buffer for incoming data
	quint64 _bytesToRead;	///< Byters left to read
	bool _playing;			///< Playing?
	int _request;			///< request counter
	GUdpReceiver	_udpReceiver; ///< Udp receiver
	
};

#endif
